<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class member
{
	public $table;
	public $pages;
	public $db;
	public $error;
	public $current_user;

	function member()
	{
		global $db,$current_user;
        $this->db = &$db;
        $this->table = DB_PRE.'member';
        $this->current_user = $current_user;
	}
    
    function get_members($params,$page,$pagesize)
    {
        $where = array();
        $data = array();
        if(isset($params['email']))
        {
            $where[] = 'email=?';
            $data[] = array('string',$params['email']);
        }
        if(isset($params['truename']))
        {
            $where[] = 'truename like ?';
            $data[] = array('string','%'.$params['truename'].'%');
        }
        if(isset($params['tel']))
        {
            $where[] = 'tel=?';
            $data[] = array('string',$params['tel']);
        }
        if(isset($params['status']))
        {
            $where[] = 'status=?';
            $data[] = array('int',$params['status']);
        }
        if(isset($params['major']))
        {
            $where[] = 'major=?';
            $data[] = array('string',$params['major']);
        }
        if(isset($params['class']))
        {
            $where[] = 'class=?';
            $data[] = array('string',$params['class']);
        }
        $where_str = '';
        if(sizeof($where))
        {
            $where_str = ' where ' . implode(' and ', $where);
        }
        
        $this->db->prepare( "SELECT count(*) as nums FROM $this->table" . $where_str );
        if(sizeof($data))
        {
            foreach($data as $d)
            {
                $func = 'set_' .$d[0];
                $this->db->$func($d[1]);
            }
        }
		$res = $this->db->execute( true );
		$nums = isset( $res['nums'] ) ? intval( $res['nums'] ) : 0;
        $this->pages = pages( $nums, $page, $pagesize );
		$this->db->prepare( "SELECT * FROM $this->table" . $where_str . " limit ?, ?" );
        if(sizeof($data))
        {
            foreach($data as $d)
            {
                $func = 'set_' .$d[0];
                $this->db->$func($d[1]);
            }
        }
		$this->db->set_int( ( $page-1 ) * $pagesize );
		$this->db->set_int( $pagesize );
	    $list = $this->db->execute();
        return $list;
    }
    
    function get_member_by_id($id)
    {
        $this->db->prepare('select * from ' . $this->table . ' where mid=?');
        $this->db->set_int($id);
        $res = $this->db->execute(true);
        return $res;
    }
    
    function get_member_by_email($email)
    {
        $this->db->prepare('select * from ' . $this->table . ' where email=?');
        $this->db->set_string($email);
        $res = $this->db->execute(true);
        return $res;
    }
    
    function add_member($data)
    {
        $res = $this->db->insert( $this->table, $data );
		return $res;
    }
    
    public function edit_member( $data, $id )
    {
		$res = $this->db->simple_update( $this->table, $data, array( 'mid'=>$id ) );
		return $res;
    }
    
    public function delete_member( $id )
    {
		$this->db->prepare( 'delete from ' . $this->table . ' where mid=?' );
        $this->db->set_int($id);
        $res = $this->db->execute();
		return $res;
    }
    
    public function get_truename($id)
    {
        $info = $this->get_member_by_id($id);
        if(empty($info)) return false;
        return $info['truename'];
    }
    

}

class member_photo
{
	public $table;
	public $pages;
	public $db;
	public $error;
	public $current_user;

	function member_photo()
	{
		global $db,$current_user;
        $this->db = &$db;
        $this->table = DB_PRE.'member_photo';
        $this->current_user = $current_user;
	}
    
    function get_photo($params,$page,$pagesize)
    {
        $where = array();
        $data = array();
        if(isset($params['mid']))
        {
            $where[] = 'mid=?';
            $data[] = array('int',$params['mid']);
        }
        if(isset($params['phototype']))
        {
            $where[] = 'phototype=?';
            $data[] = array('int',$params['phototype']);
        }
        
        $where_str = '';
        if(sizeof($where))
        {
            $where_str = ' where ' . implode(' and ', $where);
        }
        
        $this->db->prepare( "SELECT count(*) as nums FROM $this->table" . $where_str );
        if(sizeof($data))
        {
            foreach($data as $d)
            {
                $func = 'set_' .$d[0];
                $this->db->$func($d[1]);
            }
        }
		$res = $this->db->execute( true );
		$nums = isset( $res['nums'] ) ? intval( $res['nums'] ) : 0;
        $this->pages = pages( $nums, $page, $pagesize );
		$this->db->prepare( "SELECT * FROM $this->table" . $where_str . " order by addtime desc limit ?, ?" );
        if(sizeof($data))
        {
            foreach($data as $d)
            {
                $func = 'set_' .$d[0];
                $this->db->$func($d[1]);
            }
        }
		$this->db->set_int( ( $page-1 ) * $pagesize );
		$this->db->set_int( $pagesize );
	    $list = $this->db->execute();
        return $list;
    }
    
    function get_photo_by_id($id)
    {
        $this->db->prepare('select * from ' . $this->table . ' where id=?');
        $this->db->set_int($id);
        $res = $this->db->execute(true);
        return $res;
    }
    
    
    function add_photo($data)
    {
        $res = $this->db->insert( $this->table, $data );
		return $res;
    }
    
    public function edit_photo( $data, $id )
    {
		$res = $this->db->simple_update( $this->table, $data, array( 'id'=>$id ) );
		return $res;
    }
    
    public function delete_photo( $id )
    {
		$this->db->prepare( 'delete from ' . $this->table . ' where id=?' );
        $this->db->set_int($id);
        $res = $this->db->execute();
		return $res;
    }
}

class member_ly
{
	public $table;
	public $pages;
	public $db;
	public $error;
	public $current_user;

	function member_ly()
	{
		global $db,$current_user;
        $this->db = &$db;
        $this->table = DB_PRE.'member_ly';
        $this->current_user = $current_user;
	}
    
    function get_ly($params,$page,$pagesize)
    {
        $where = array();
        $data = array();
        if(isset($params['mid']))
        {
            $where[] = 'mid=?';
            $data[] = array('int',$params['mid']);
        }
        if(isset($params['type']))
        {
            $where[] = 'type=?';
            $data[] = array('string',$params['type']);
        }
        if(isset($params['status']))
        {
            $where[] = 'status=?';
            $data[] = array('int',$params['status']);
        }
        
        $where_str = '';
        if(sizeof($where))
        {
            $where_str = ' where ' . implode(' and ', $where);
        }
        
        $this->db->prepare( "SELECT count(*) as nums FROM $this->table" . $where_str );
        if(sizeof($data))
        {
            foreach($data as $d)
            {
                $func = 'set_' .$d[0];
                $this->db->$func($d[1]);
            }
        }
		$res = $this->db->execute( true );
		$nums = isset( $res['nums'] ) ? intval( $res['nums'] ) : 0;
        $this->pages = pages( $nums, $page, $pagesize );
		$this->db->prepare( "SELECT * FROM $this->table" . $where_str . " order by addtime desc limit ?, ?" );
        if(sizeof($data))
        {
            foreach($data as $d)
            {
                $func = 'set_' .$d[0];
                $this->db->$func($d[1]);
            }
        }
		$this->db->set_int( ( $page-1 ) * $pagesize );
		$this->db->set_int( $pagesize );
	    $list = $this->db->execute();
        return $list;
    }
    
    function get_ly_by_id($id)
    {
        $this->db->prepare('select * from ' . $this->table . ' where id=?');
        $this->db->set_int($id);
        $res = $this->db->execute(true);
        return $res;
    }
    
    
    function add_ly($data)
    {
        $res = $this->db->insert( $this->table, $data );
		return $res;
    }
    
    public function edit_ly( $data, $id )
    {
		$res = $this->db->simple_update( $this->table, $data, array( 'id'=>$id ) );
		return $res;
    }
    
    public function delete_ly( $id )
    {
		$this->db->prepare( 'delete from ' . $this->table . ' where id=?' );
        $this->db->set_int($id);
        $res = $this->db->execute();
		return $res;
    }
}

class member_message
{
	public $table;
	public $pages;
	public $db;
	public $error;
	public $current_user;

	function member_message()
	{
		global $db,$current_user;
        $this->db = &$db;
        $this->table = DB_PRE.'member_message';
        $this->current_user = $current_user;
	}
    
    function get_message($params)
    {
        $where = array();
        $data = array();
        if(isset($params['fromuser']))
        {
            $where[] = 'fromuser=?';
            $data[] = array('int',$params['fromuser']);
        }
        if(isset($params['touser']))
        {
            $where[] = 'touser=?';
            $data[] = array('int',$params['touser']);
        }
      
        
        $where_str = '';
        if(sizeof($where))
        {
            $where_str = ' where ' . implode(' and ', $where);
        }
        
        $this->db->prepare( "SELECT count(*) as nums FROM $this->table" . $where_str );
        if(sizeof($data))
        {
            foreach($data as $d)
            {
                $func = 'set_' .$d[0];
                $this->db->$func($d[1]);
            }
        }
		$res = $this->db->execute( true );
		$nums = isset( $res['nums'] ) ? intval( $res['nums'] ) : 0;
		$this->db->prepare( "SELECT * FROM $this->table" . $where_str . " order by addtime desc" );
        if(sizeof($data))
        {
            foreach($data as $d)
            {
                $func = 'set_' .$d[0];
                $this->db->$func($d[1]);
            }
        }
	    $list = $this->db->execute();
        return $list;
    }
    
    function get_message_by_id($id)
    {
        $this->db->prepare('select * from ' . $this->table . ' where id=?');
        $this->db->set_int($id);
        $res = $this->db->execute(true);
        return $res;
    }
    
    
    function add_message($data)
    {
        $res = $this->db->insert( $this->table, $data );
		return $res;
    }
    
    public function edit_message( $data, $id )
    {
		$res = $this->db->simple_update( $this->table, $data, array( 'id'=>$id ) );
		return $res;
    }
    
    public function delete_message( $id )
    {
		$this->db->prepare( 'delete from ' . $this->table . ' where id=?' );
        $this->db->set_int($id);
        $res = $this->db->execute();
		return $res;
    }
}
?>
